﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.BGM;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Data.Entity.Migrations;

namespace KPIS.GERP.WCF.GBMS
{
    [ServiceContract]
    public interface IBgmSystemControlService
    {
        [OperationContract]
        IEnumerable<SystemControl> Select(SystemControl criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int BgYear();

        [OperationContract]
        int JobSiteStrategy();

        [OperationContract]
        int ReqBgYear();

        [OperationContract]
        int JobSiteOption1();

        [OperationContract]

        int JobSiteOption2();

        [OperationContract]

        int JobSiteOption3();

        [OperationContract]
        void UpdateBgYear(object value);

        [OperationContract]
        void UpdateJobSiteStrategy(object value);

        [OperationContract]
        void UpdateReqBgYear(object value);

        [OperationContract]
        void UpdateJobSiteOption1(object value);

        [OperationContract]

        void UpdateJobSiteOption2(object value);

        [OperationContract]
        void UpdateJobSiteOption3(object value);

        [OperationContract]
        int SelectInt(string systemControlName);

        [OperationContract]
        void Update(string systemControlName, object value);
    }

    public class BgmSystemControlService : IBgmSystemControlService
    {
        public IEnumerable<SystemControl> Select(SystemControl criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<SystemControl> query = context.SystemControls;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private int Get(params string[] key)
        {
            var a = Select();
            var b = (from sc in a where key.Contains(sc.BgmSysControlName) select sc.BgmSysControlValues).ToList();
            if(b.Count == 0) throw new Exception("System control parameter not configured properly");
            var c = b.FirstOrDefault();
            var d = Models.Class.ConvertUtility.ConvertOrDefaultObjectInt(c);
            return d;
        }

        public int BgYear()
        {
            return Get("BgYear", "bg_year");
        }

        public int JobSiteStrategy()
        {
            return Get("JobSiteStrategy", "job_site_strategy");
        }

        public int ReqBgYear()
        {
            return Get("ReqBgYear", "req_bg_year");
        }

        public int JobSiteOption1()
        {
            return Get("JobSiteOption1", "job_site_option1");
        }

        public int JobSiteOption2()
        {
            return Get("JobSiteOption2", "job_site_option2");
        }

        public int JobSiteOption3()
        {
            return Get("JobSiteOption3", "job_site_option3");
        }

        public void UpdateBgYear(object value)
        {
            Update("BgYear", value);
        }

        public void UpdateJobSiteStrategy(object value)
        {
            Update("JobSiteStrategy", value);
        }

        public void UpdateReqBgYear(object value)
        {
            Update("ReqBgYear", value);
        }

        public void UpdateJobSiteOption1(object value)
        {
            Update("JobSiteOption1", value);
        }

        public void UpdateJobSiteOption2(object value)
        {
            Update("JobSiteOption2", value);
        }

        public void UpdateJobSiteOption3(object value)
        {
            Update("JobSiteOption3", value);
        }
        
        public void Update(string systemControlName, object value)
        {
            using (var context = new GERPContext(false))
            {
                var firstObj = context.SystemControls.FirstOrDefault(o => o.BgmSysControlName == systemControlName) ?? new SystemControl();
                firstObj.BgmSysControlValues = Convert.ToString(value);
                context.SystemControls.AddOrUpdate(firstObj);
            }
        }
        
        public string Select(string systemControlName)
        {
            using (var context = new GERPContext(false))
            {
                var firstObj = context.SystemControls.FirstOrDefault(o => o.BgmSysControlName == systemControlName);
                return firstObj == null ? null : firstObj.BgmSysControlValues;
            }
        }
        
        public int SelectInt(string systemControlName)
        {
            return Models.Class.ConvertUtility.ConvertOrDefaultObjectInt(Select(systemControlName));
        }
    }
}
